package com.study.easyexcel;

import com.study.easyexcel.entity.DetailData;
import com.study.easyexcel.entity.TemplateData;
import com.study.easyexcel.fill.FillUtil;
import com.study.easyexcel.read.ReadUtil;

import java.util.*;
import java.util.stream.Collectors;

// 按两次 Shift 打开“随处搜索”对话框并输入 `show whitespaces`，
// 然后按 Enter 键。现在，您可以在代码中看到空格字符。
public class Main {
    public static void main(String[] args) throws Exception {
        String fileName = "0075T231021-200739-101029[001].xlsx";//原始明细数据文件
        String sheetName = "明细数据(029)";//sheet页签标题
        String templateFile = "A-template.xlsx";//生成的模板文件
        String outFile = "";//为空则代表自动生成文件

        generateData1(fileName,sheetName,templateFile,outFile);
    }


    public static void generateData(String fileName,String sheetName,String templateFile, String outFile) throws Exception {

        String dxm = fileName.substring(0, fileName.indexOf("["));
        //第1步：读取Excel文件
        List<DetailData> data = ReadUtil.readData(fileName, sheetName);

        //第2步：整理数据 组织填充数据
        Map<String, List<DetailData>> map = data.stream()
                .filter(s ->!"搁置".equals(s.getMode()))
                .collect(Collectors.groupingBy(DetailData::getMode));

        Iterator<String> iterator = map.keySet().iterator();
        List<TemplateData> templateData = new ArrayList<>();
        while (iterator.hasNext()) {
            String next = iterator.next();
            if (next.endsWith("充电")) {//数据放到左边
                List<DetailData> dataList = map.get(next);
                DetailData maxWd1 = dataList.stream().max((o1, o2) -> o1.getWd1().compareTo(o2.getWd1())).get();
                DetailData minWd1 = dataList.stream().min((o1, o2) -> o1.getWd1().compareTo(o2.getWd1())).get();
                DetailData maxWd2 = dataList.stream().max((o1, o2) -> o1.getWd2().compareTo(o2.getWd2())).get();
                DetailData minWd2 = dataList.stream().min((o1, o2) -> o1.getWd2().compareTo(o2.getWd2())).get();
                DetailData maxWd3 = dataList.stream().max((o1, o2) -> o1.getWd3().compareTo(o2.getWd3())).get();
                DetailData minWd3 = dataList.stream().min((o1, o2) -> o1.getWd3().compareTo(o2.getWd3())).get();
                DetailData maxWd4 = dataList.stream().max((o1, o2) -> o1.getWd4().compareTo(o2.getWd4())).get();
                DetailData minWd4 = dataList.stream().min((o1, o2) -> o1.getWd4().compareTo(o2.getWd4())).get();
                TemplateData a = new TemplateData();
                a.setDxtm(dxm);
                a.setXhh(dataList.get(0).getXhh());
                a.setMode(next);
                a.setWd1max(maxWd1.getWd1());
                a.setWd1min(minWd1.getWd1());
                a.setWd1cha(cha(maxWd1.getWd1(), minWd1.getWd1()));
                a.setWd2max(maxWd2.getWd2());
                a.setWd2min(minWd2.getWd2());
                a.setWd2cha(cha(maxWd2.getWd2(), minWd2.getWd2()));
                a.setWd3max(maxWd3.getWd3());
                a.setWd3min(minWd3.getWd3());
                a.setWd3cha(cha(maxWd3.getWd3(), minWd3.getWd3()));
                a.setWd4max(maxWd4.getWd4());
                a.setWd4min(minWd4.getWd4());
                a.setWd4cha(cha(maxWd4.getWd4(), minWd4.getWd4()));

                templateData.add(a);

            }
        }

        Iterator<String> iterator2 = map.keySet().iterator();
        int i=0;
        while (iterator2.hasNext()) {
            String next = iterator2.next();
            if (next.endsWith("放电")) {//数据放到右边
                TemplateData data1 = templateData.get(i);
                if(templateData.size()>i){
                    data1 = templateData.get(i);
                }else{
                    data1 = new TemplateData();
                    templateData.add(data1);
                    i++;
                }
                List<DetailData> dataList = map.get(next);

                DetailData maxHot1 = dataList.stream().max((o1, o2) -> o1.getWd1().compareTo(o2.getWd1())).get();
                DetailData minHot1 = dataList.stream().min((o1, o2) -> o1.getWd1().compareTo(o2.getWd1())).get();

                DetailData maxHot2 = dataList.stream().max((o1, o2) -> o1.getWd2().compareTo(o2.getWd2())).get();
                DetailData minHot2 = dataList.stream().min((o1, o2) -> o1.getWd2().compareTo(o2.getWd2())).get();

                DetailData maxHot3 = dataList.stream().max((o1, o2) -> o1.getWd3().compareTo(o2.getWd3())).get();
                DetailData minHot3 = dataList.stream().min((o1, o2) -> o1.getWd3().compareTo(o2.getWd3())).get();


                DetailData maxHot4 = dataList.stream().max((o1, o2) -> o1.getWd4().compareTo(o2.getWd4())).get();
                DetailData minHot4 = dataList.stream().min((o1, o2) -> o1.getWd4().compareTo(o2.getWd4())).get();

                data1.setDxtm(dxm);
                data1.setXhh1(dataList.get(0).getXhh());
                data1.setMode1(next);
                data1.setHot1max(maxHot1.getWd1());
                data1.setHot1min(minHot1.getWd1());
                data1.setHot1cha(cha(maxHot1.getWd1(), minHot1.getWd1()));
                data1.setHot2max(maxHot2.getWd2());
                data1.setHot2min(minHot2.getWd2());
                data1.setHot2cha(cha(maxHot2.getWd2(), minHot2.getWd2()));
                data1.setHot3max(maxHot3.getWd3());
                data1.setHot3min(minHot3.getWd3());
                data1.setHot3cha(cha(maxHot3.getWd3(), minHot3.getWd3()));
                data1.setHot4max(maxHot4.getWd4());
                data1.setHot4min(minHot4.getWd4());
                data1.setHot4cha(cha(maxHot4.getWd4(), minHot4.getWd4()));

            }
        }

        //第3步：填充数据
        System.out.println("准备填充的长度 = "+templateData.size());
        FillUtil.fillExcel(templateData,templateFile,outFile);
    }


    public static void generateData1(String fileName,String sheetName,String templateFile, String outFile) throws Exception {

        String dxm = fileName.substring(0, fileName.indexOf("["));
        //第1步：读取Excel文件
        List<DetailData> data = ReadUtil.readData(fileName, sheetName);

        //第2步：整理数据 组织填充数据
        Map<String, Map<Integer, List<DetailData>>> collectTemp = data.stream()
                .filter(s -> !"搁置".equals(s.getMode()))
                .collect(Collectors.groupingBy(DetailData::getMode, Collectors.groupingBy(DetailData::getXhh)));
        //排序
        Map<String, Map<Integer, List<DetailData>>> sortMap = new HashMap<>();
        collectTemp.forEach((k,v)->{
            Map<Integer, List<DetailData>> t = new HashMap<>();
            v.forEach((a,b)->{
                List<DetailData> sort = b.stream().sorted((d1, d2) -> d1.getXhh().compareTo(d2.getXhh())).collect(Collectors.toList());
                t.put(a,sort);
            });
            sortMap.put(k,t);
        });




        Iterator<String> iterator = sortMap.keySet().iterator();
        List<TemplateData> templateData = new ArrayList<>();
        while (iterator.hasNext()) {
            String next = iterator.next();
            if (next.endsWith("充电")) {//数据放到左边
                Map<Integer, List<DetailData>> xhhMap = sortMap.get(next);
                xhhMap.forEach((k,v)->{
                    DetailData maxWd1 = v.stream().max((o1, o2) -> o1.getWd1().compareTo(o2.getWd1())).get();
                    DetailData minWd1 = v.stream().min((o1, o2) -> o1.getWd1().compareTo(o2.getWd1())).get();
                    DetailData maxWd2 = v.stream().max((o1, o2) -> o1.getWd2().compareTo(o2.getWd2())).get();
                    DetailData minWd2 = v.stream().min((o1, o2) -> o1.getWd2().compareTo(o2.getWd2())).get();
                    DetailData maxWd3 = v.stream().max((o1, o2) -> o1.getWd3().compareTo(o2.getWd3())).get();
                    DetailData minWd3 = v.stream().min((o1, o2) -> o1.getWd3().compareTo(o2.getWd3())).get();
                    DetailData maxWd4 = v.stream().max((o1, o2) -> o1.getWd4().compareTo(o2.getWd4())).get();
                    DetailData minWd4 = v.stream().min((o1, o2) -> o1.getWd4().compareTo(o2.getWd4())).get();

                    TemplateData a = new TemplateData();
                    a.setDxtm(dxm);
                    a.setXhh(k);
                    a.setMode(next);
                    a.setWd1max(maxWd1.getWd1());
                    a.setWd1min(minWd1.getWd1());
                    a.setWd1cha(cha(maxWd1.getWd1(), minWd1.getWd1()));
                    a.setWd2max(maxWd2.getWd2());
                    a.setWd2min(minWd2.getWd2());
                    a.setWd2cha(cha(maxWd2.getWd2(), minWd2.getWd2()));
                    a.setWd3max(maxWd3.getWd3());
                    a.setWd3min(minWd3.getWd3());
                    a.setWd3cha(cha(maxWd3.getWd3(), minWd3.getWd3()));
                    a.setWd4max(maxWd4.getWd4());
                    a.setWd4min(minWd4.getWd4());
                    a.setWd4cha(cha(maxWd4.getWd4(), minWd4.getWd4()));
                    templateData.add(a);
                });

            }
        }

        Iterator<String> iterator2 = sortMap.keySet().iterator();
        int i=0;
        while (iterator2.hasNext()) {
            String next = iterator2.next();
            if (next.endsWith("放电")) {//数据放到右边
                Map<Integer, List<DetailData>> xhhMap = sortMap.get(next);
                Iterator<Integer> it1 = xhhMap.keySet().iterator();
                while(it1.hasNext()){
                    Integer xhh1 = it1.next();
                    List<DetailData> t = xhhMap.get(xhh1);

                    DetailData maxHot1 = t.stream().max((o1, o2) -> o1.getWd1().compareTo(o2.getWd1())).get();
                    DetailData minHot1 = t.stream().min((o1, o2) -> o1.getWd1().compareTo(o2.getWd1())).get();
                    DetailData maxHot2 = t.stream().max((o1, o2) -> o1.getWd2().compareTo(o2.getWd2())).get();
                    DetailData minHot2 = t.stream().min((o1, o2) -> o1.getWd2().compareTo(o2.getWd2())).get();
                    DetailData maxHot3 = t.stream().max((o1, o2) -> o1.getWd3().compareTo(o2.getWd3())).get();
                    DetailData minHot3 = t.stream().min((o1, o2) -> o1.getWd3().compareTo(o2.getWd3())).get();
                    DetailData maxHot4 = t.stream().max((o1, o2) -> o1.getWd4().compareTo(o2.getWd4())).get();
                    DetailData minHot4 = t.stream().min((o1, o2) -> o1.getWd4().compareTo(o2.getWd4())).get();

                    TemplateData data1 = templateData.get(i);
                    if(templateData.size()>i){
                        data1 = templateData.get(i);
                    }else{
                        data1 = new TemplateData();
                        templateData.add(data1);
                        i++;
                    }

                    data1.setDxtm(dxm);
                    data1.setXhh1(xhh1);
                    data1.setMode1(next);
                    data1.setHot1max(maxHot1.getWd1());
                    data1.setHot1min(minHot1.getWd1());
                    data1.setHot1cha(cha(maxHot1.getWd1(), minHot1.getWd1()));
                    data1.setHot2max(maxHot2.getWd2());
                    data1.setHot2min(minHot2.getWd2());
                    data1.setHot2cha(cha(maxHot2.getWd2(), minHot2.getWd2()));
                    data1.setHot3max(maxHot3.getWd3());
                    data1.setHot3min(minHot3.getWd3());
                    data1.setHot3cha(cha(maxHot3.getWd3(), minHot3.getWd3()));
                    data1.setHot4max(maxHot4.getWd4());
                    data1.setHot4min(minHot4.getWd4());
                    data1.setHot4cha(cha(maxHot4.getWd4(), minHot4.getWd4()));

                }
            }
        }

        //第3步：填充数据
        System.out.println("准备填充的长度 = "+templateData.size());
        FillUtil.fillExcel(templateData,templateFile,outFile);
    }

    private static Double cha(Double a,Double b){
        if(a==null && b==null){
            return 0d;
        }else if(a!=null && b==null ){
            return a;
        }else if(a ==null && b!=null ){
            return -b;
        }else{
            return a-b;
        }
    }


}